/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rmsdao;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import rmsbean.EventoBean;
import rmsbean.UsuarioBean;
/**
 *
 * @author luis
 */
public class EventoDAO {
    private static final String RMSSTORE = "RMSEVENTO";
    private RecordStore agendaRMS;

    public void open() throws RecordStoreException {
        this.agendaRMS = RecordStore.openRecordStore(RMSSTORE, true);
        System.out.println("EVENTO CRIOUUUUUU");
    }

    public void close() throws RecordStoreNotOpenException, RecordStoreException {
        agendaRMS.closeRecordStore();
    }

    public void clear() throws RecordStoreException {
        RecordStore.deleteRecordStore(RMSSTORE);
    }

    public void insert(EventoBean evt) throws IOException, RecordStoreNotOpenException, RecordStoreException {
        this.open();
        //login, descricao, data
        byte[] registroBytes = evt.getBytes();
        int cod = agendaRMS.addRecord(registroBytes, 0, registroBytes.length);
        System.out.println("Fez insert no rms");
        this.close();
    }
    
    public void delete(EventoBean evt) throws IOException, RecordStoreNotOpenException, RecordStoreException {
        this.open();
        agendaRMS.deleteRecord(evt.getCod());
        this.close();
    }
    
    public void update(EventoBean evt) throws IOException, RecordStoreNotOpenException, RecordStoreException {
        this.open();
        byte[] registroBytes = evt.getBytes();
        agendaRMS.setRecord(evt.getCod(), registroBytes, 0, registroBytes.length);
        this.close();
    }
    
    public Vector todos() throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, IOException {
        Vector ret = new Vector();
        RecordEnumeration enumerator = agendaRMS.enumerateRecords(null, null, false);
        while (enumerator.hasNextElement()) {
            UsuarioBean listaBean = new UsuarioBean();
            listaBean.setCod(enumerator.nextRecordId());
            listaBean.setBytes(agendaRMS.getRecord(listaBean.getCod()));
            ret.addElement(listaBean);
            
        }
        return ret;
    }
    
}
